package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by jrocha on 9/23/14.
 */
public class Menu {
    private static final int MAIN_MENU = 1;
    private static final int LIST_BOOKS_MENU = 2;
    private static final int LIST_MOVIES_MENU = 3;
    private static final int RETURNBOOK_MENU = 4;
    private static final int RETURNMOVIE_MENU = 5;
    private static final int CHECKOUTBOOK_MENU = 6;
    private static final int CHECKOUTMOVIE_MENU = 7;
    private static final int USER_INFORMATION = 8;

    public Menu() {
        this(new User("", "", "", "", ""));
    }

    public Menu(User user){
        this.mainMenuOptions = new ArrayList<ItemMenu>();
        this.mainMenuOptions.add(new ItemMenu(2, "List Books", 1));
        this.mainMenuOptions.add(new ItemMenu(3, "List Movies", 1));
        this.mainMenuOptions.add(new ItemMenu(4, "Return Book", 1));
        this.mainMenuOptions.add(new ItemMenu(5, "Return Movie", 1));
        this.mainMenuOptions.add(new ItemMenu(8, "Return Movie", 1));
        this.mainMenuOptions.add(new ItemMenu(0, "Quit", 1));

        this.listBooksMenuOptions = new ArrayList<ItemMenu>();
        this.listBooksMenuOptions.add(new ItemMenu(6, "Checkout Book", 2));
        this.listBooksMenuOptions.add(new ItemMenu(1, "Previous Menu", 2));
        this.listBooksMenuOptions.add(new ItemMenu(0, "Quit", 2));

        this.listMoviesMenuOptions = new ArrayList<ItemMenu>();
        this.listMoviesMenuOptions.add(new ItemMenu(7, "Checkout Movie", 3));
        this.listMoviesMenuOptions.add(new ItemMenu(1, "Previous Menu", 3));
        this.listMoviesMenuOptions.add(new ItemMenu(0, "Quit", 3));

        this.stateMenu = MAIN_MENU;

        this.library= new Library(user);
        this.messageMenu = new MessageMenu();
    }

    public MessageMenu showMainMenu() {
        messageMenu.setMessage("");
        messageMenu.setMenu(mainMenuOptions);
        return messageMenu;
    }

    public void showListBooksMenu() {
        ArrayList<ItemLibrary> books = library.listBooksAvailable();
        messageMenu.setItens(books);
    }

    public boolean validOption(String menuId){
        try {
            Integer.parseInt(menuId);
            return true;
        }
        catch (NumberFormatException nfe){
            return false;
        }
    }

    public MessageMenu chooseMenu(String menuId) {
        int menuIdInt;
        if (validOption(menuId))
            menuIdInt = Integer.parseInt(menuId);
        else {
            messageMenu.setMessage("Opcao nao valida");
            return messageMenu;
        }

        messageMenu.setMenu(new ArrayList<ItemMenu>());
        messageMenu.setMessage("");
        messageMenu.setItens(new ArrayList<ItemLibrary>());

        switch (stateMenu) {
            case MAIN_MENU:
                mainMenu(menuIdInt);
                break;
            case LIST_BOOKS_MENU:
                listBooksMenu(menuIdInt);
                break;
            case RETURNBOOK_MENU:
                returnBookMenu(menuIdInt);
                break;
            case CHECKOUTBOOK_MENU:
                checkoutBookMenu(menuIdInt);
                break;
            case LIST_MOVIES_MENU:
                listMoviesMenu(menuIdInt);
                break;
            case RETURNMOVIE_MENU:
                returnMovieMenu(menuIdInt);
                break;
            case CHECKOUTMOVIE_MENU:
                checkoutMovieMenu(menuIdInt);
                break;
            default:
                messageMenu.setMessage("Opcao nao existente");
        }

        return messageMenu;
    }

    private void checkoutBookMenu(int bookId) {
        ArrayList<ItemMenu> menu;
        checkoutBook(bookId);
        this.stateMenu = LIST_BOOKS_MENU;
        showListBooksMenu();
        menu = listBooksMenuOptions;
        messageMenu.setMenu(menu);
    }

    private void returnBookMenu(int bookId) {
        ArrayList<ItemMenu> menu;
        returnBook(bookId);
        this.stateMenu = MAIN_MENU;
        menu = mainMenuOptions;
        messageMenu.setMenu(menu);
    }

    public void mainMenu(int subMenu) {
        ArrayList<ItemMenu> menu;
        switch (subMenu) {
            case LIST_BOOKS_MENU:
                showListBooksMenu();
                menu = listBooksMenuOptions;
                messageMenu.setMenu(menu);
                this.stateMenu = subMenu;
                break;
            case LIST_MOVIES_MENU:
                showListMoviesMenu();
                menu = listMoviesMenuOptions;
                messageMenu.setMenu(menu);
                this.stateMenu = subMenu;
                break;
            case RETURNBOOK_MENU:
                this.stateMenu = subMenu;
                break;
            case RETURNMOVIE_MENU:
                this.stateMenu = subMenu;
                break;
            case USER_INFORMATION:
                showUserInformation();
                break;
            default:
                messageMenu.setMessage("Opcao nao existente");
        }
    }

    private void showUserInformation() {
        String userInformation = library.showUserInformation();
        messageMenu.setUserInformation(userInformation);
        this.stateMenu = MAIN_MENU;
        ArrayList<ItemMenu> menu;
        menu = mainMenuOptions;
        messageMenu.setMenu(menu);
    }

    public void listBooksMenu(int subMenu) {
        ArrayList<ItemMenu> menu;

        switch (subMenu) {
            case CHECKOUTBOOK_MENU:
                this.stateMenu = subMenu;
                break;
            case MAIN_MENU:
                this.stateMenu = MAIN_MENU;
                menu = mainMenuOptions;
                messageMenu.setMenu(menu);
                break;
            default:
                messageMenu.setMessage("Opcao nao existente");
        }
    }


    public void listMoviesMenu(int subMenu) {
        ArrayList<ItemMenu> menu;

        switch (subMenu) {
            case CHECKOUTMOVIE_MENU:
                this.stateMenu = subMenu;
                break;
            case MAIN_MENU:
                this.stateMenu = MAIN_MENU;
                menu = mainMenuOptions;
                messageMenu.setMenu(menu);
                break;
            default:
                messageMenu.setMessage("Opcao nao existente");
        }
    }

    private void checkoutMovieMenu(int movieId) {
        ArrayList<ItemMenu> menu;
        checkoutMovies(movieId);
        this.stateMenu = LIST_MOVIES_MENU;
        showListMoviesMenu();
        menu = listMoviesMenuOptions;
        messageMenu.setMenu(menu);
    }

    private void returnMovieMenu(int movieId) {
        ArrayList<ItemMenu> menu;
        returnMovies(movieId);
        this.stateMenu = MAIN_MENU;
        menu = mainMenuOptions;
        messageMenu.setMenu(menu);
    }

    public void checkoutBook(int bookId) {
        String messageCheckout = library.checkOutBook(bookId);
        messageMenu.setMessage(messageCheckout);

    }

    public void returnBook(int bookId) {
        String messageReturn = library.returnBook(bookId);
        messageMenu.setMessage(messageReturn);

    }

    public void showListMoviesMenu() {
        ArrayList<ItemLibrary> movies = library.listMoviesAvailable();
        messageMenu.setItens(movies);
    }

    public void checkoutMovies(int movieId) {
        String messageCheckout = library.checkOutMovie(movieId);
        messageMenu.setMessage(messageCheckout);

    }

    public void returnMovies(int movieId) {
        String messageReturn = library.returnMovie(movieId);
        messageMenu.setMessage(messageReturn);

    }

    private int stateMenu;

    private ArrayList<ItemMenu> mainMenuOptions;
    private ArrayList<ItemMenu> listBooksMenuOptions;
    private ArrayList<ItemMenu> listMoviesMenuOptions;

    private Library library;

    private MessageMenu messageMenu;
}
